/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.UserService;
import etcore.webapp.grouptalk.util.MD5;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * This action validates the request and executes the command of activating a
 * user,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ActivateUserAction extends AbstractAction {

	private static final long serialVersionUID = 9071865462818616268L;
	// validate result
	public static final int ACTIVATE_SUCCESS = 0;
	public static final int ACTIVATE_ERROR = 1;

	public static final String FAILURE_PAGE = "/error.jsp";
	public static final String USER_CONTROL_PAGE = "/group/myInfo.action";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String userName = request.getParameter(PageConstants.USERNAME);
		String code = request.getParameter(PageConstants.CODE);
		MD5 md5 = new MD5();

		String cmd = request.getParameter(PageConstants.CMD);
		if (PageConstants.PAGE_NAME_ACTIVATEUSER.equals(cmd)) {
			if (userName != null && code != null
					&& !userName.trim().equals(Symbols.BLANK)
					&& !code.trim().equals(Symbols.BLANK)) {

				UserService userService = new UserService();

				User user = userService.findByUserName(userName);
				if (user != null)// code
									// matches
				{
					String system_code = md5.getMD5ofStr(user.getEmail()
							.substring(0, user.getEmail().indexOf("@")))
							+ "_"
							+ md5.getMD5ofStr(user.getEmail().substring(
									user.getEmail().indexOf("@") + 1));

					if (system_code != null && system_code.equals(code.trim())) {
						user.setIsLock(0);
						user.setCode(Symbols.BLANK);
						user = (User) userService.merge(user);
						request.setAttribute(PageConstants.SESSIONUSER, user);

						return ACTIVATE_SUCCESS;
					}
				}

			}
		}
		return ACTIVATE_ERROR;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (ACTIVATE_SUCCESS == validationResult) {
			return PageConstants.PAGE_ACTION_MYINFO;
		} else {
			return PageConstants.ACTION_PATH_ERROR;
		}
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		HttpSession session = request.getSession(true);

		String lang = (String) session.getAttribute(Messages.LANG);
		if (ACTIVATE_SUCCESS == validationResult) {
			if (lang != null && lang.equals(Messages.LANG_EN)) {
				msg = Messages.getELabel(Messages.ACTIVATION__SUCCESS);
			} else {
				msg = Messages.getLabel(Messages.ACTIVATION__SUCCESS);
			}

		} else {
			if (lang != null && lang.equals(Messages.LANG_EN)) {
				msg = Messages.getELabel(Messages.ACTIVATION__ERROR);
			} else {
				msg = Messages.getLabel(Messages.ACTIVATION__ERROR);
			}
		}
		request.setAttribute(PageConstants.MSG, msg);
	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
		return null;
	}

}
